﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.CompanyName = ko.observable(data.PartnerInfo.CompanyName).extend({ maxLength: 250 }).extend({ required: { message: "Name is required"} });
    this.ContactTitle = ko.observable(data.PartnerInfo.ContactTitle).extend({ maxLength: 50 }).extend({ required: { message: "Contact Title is required"} });
    this.EmailAddress = ko.observable(data.PartnerInfo.EmailAddress).extend({ required: { message: 'Enter a valid email'} }).extend({ email: { message: 'Enter a valid email'} });

    this.Phone = ko.observable(data.PartnerInfo.Phone);
    this.Fax = ko.observable(data.PartnerInfo.Fax);
    this.Address1 = ko.observable(data.PartnerInfo.Address1);
    this.Address2 = ko.observable(data.PartnerInfo.Address2);
    this.City = ko.observable(data.PartnerInfo.City);
    this.Zipcode = ko.observable(data.PartnerInfo.ZipCode);
    this.ImageUrl = ko.observable(data.PartnerInfo.ImageURL);
    this.WebsiteURL = ko.observable(data.PartnerInfo.WebsiteURL);
    this.Description = ko.observable(data.PartnerInfo.Description);
    this.Active = ko.observable(data.PartnerInfo.Active);

    this.State = ko.observable(data.PartnerInfo.State);
    this.StateOptions = ko.observableArray(data.ListState);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));